<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>签到</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<link rel="stylesheet" href="../../../css/mui.min.css">
		<link rel="stylesheet" href="../../../css/global.css">
		<link rel="stylesheet" href="../../../css/style.css">
		<link href="../../../css/task.css" rel="stylesheet" />
		<style type="text/css">
			#div {
				width: 0px;
				height: 0px;
				background: red;
				position: fixed;
				top: 70%;
				left: 50%;
			}
			/*移除底部或顶部三角,需要在删除此代码*/
			.mui-popover .mui-popover-arrow:after {
				width: 0px;
			}
		</style>
	</head>

	<body>
		
		<div id="div"></div>
		<div id="popover" class="mui-popover shadow popover">
			<p class="title" id="title">你当前处于休假状态，不能签到，是否立即结束休假？</p>
			<div class="btn_box">
				
				<span class="fl" onclick="closepop()">取消</span>
				<span class="fr red" id="quedingupdate">确定</span>
			</div> 
		</div>
		
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 id="title" class="mui-title">签到</h1>
		</header>
		<div class="mui-content">
			<div class="details_con">
				<div class="clum">
					<span class="name">服务编号</span>
					<span class="txt" id="serviceNo"></span>
				</div>
				<div class="clum">
					<span class="name">提交时间</span>
					<span class="txt" id="createTime"></span>
				</div>
				<div class="clum">
					<span class="name">服务子类型</span>
					<span class="txt" id="serviceType"></span>
				</div>
				<div class="clum_arr">
					<span class="name_arr">客户名称</span>
					<span class="txt_arr" id="customerName"></span>
				</div>
				<div class="clum">
					<span class="name">客户类型</span>
					<span class="txt" id="enginnerType"></span>
				</div>
				<div class="clum nobor">
					<span class="name">期望服务时间</span>
					<span class="txt" id="hopeBusinessHours"></span>
				</div>
				<div class="clum">
					<span class="name">服务角色</span>
					<span class="txt">
						<input class="serviceRole" name="serviceRole" type="radio" value="1" disabled="disabled">现场服务&nbsp;&nbsp;
						<input class="serviceRole" name="serviceRole" type="radio" value="2" disabled="disabled">实习
					</span>
				</div>
				<div class="line"></div>
				<div class="my-content-cell">
					<div class="inline-block" id="khxxxx">
						<em>客户详细信息</em>
						<span class="mui-icon mui-icon-arrowright arrow-right-position"></span>
					</div>
				</div>
				<div class="my-content-cell">
					<div class="inline-block" id="yqxx">
						<em>仪器详细信息</em>
						<span class="mui-icon mui-icon-arrowright arrow-right-position"></span>
					</div>
				</div>
				<div class="my-content-cell">
					<div class="inline-block" id="htxx">
						<em>合同详细信息</em>
						<span class="mui-icon mui-icon-arrowright arrow-right-position"></span>
					</div>
				</div>
				<div class="my-content-cell">
					<div class="inline-block" id="fwtzxx">
						<em>服务通知详细信息</em>
						<span class="mui-icon mui-icon-arrowright arrow-right-position"></span>
					</div>
				</div>
				<div class="my-content-cell">
					<div class="inline-block" id="tjsybjxx">
						<em>推荐使用备件详细信息</em>
						<span class="mui-icon mui-icon-arrowright arrow-right-position"></span>
					</div>
				</div>
				<div class="my-content-cell">
					<div class="inline-block" id="ggyjsmsj">
						<em>更改预计上门时间</em>
						<span class="mui-icon mui-icon-arrowright arrow-right-position"></span>
					</div>
				</div>
				<div class="my-content-cell">
					<div class="inline-block" id="xxggsq">
						<em>信息更改申请</em>
						<span class="mui-icon mui-icon-arrowright arrow-right-position"></span>
					</div>
				</div>
				<div class="my-content-cell">
					<div class="inline-block" id="ckbg">
						<em>查看报告</em>
						<span class="mui-icon mui-icon-arrowright arrow-right-position"></span>
					</div>
				</div>
				<div class="my-content-cell">
					<div class="inline-block" id="ckrz">
						<em>查看日志</em>
						<span class="mui-icon mui-icon-arrowright arrow-right-position"></span>
					</div>
				</div>
				<div class="my-content-cell">
					<div class="inline-block" id="report">
						<em>Error Report&Hot Account Report</em>
						<span class="mui-icon mui-icon-arrowright arrow-right-position"></span>
					</div>
				</div>
			</div>
			<div class="btn_box">
				
				<span class="fl" id="tjrz" style="display: none;">添加日志</span>
				<span class="fl" id="tjbg" style="display: none;">添加报告</span>
				<span class="fr red" id="qd">签到</span>
			</div>
			<div style="height: 50px;"></div>
		</div>

		<!-- js代码 start -->

		<!-- 基础js start -->
		<script src="../../../js/mui.min.js"></script>
		<script src="../../../js/utils/h.js"></script>
		<script src="../../../js/constant.js"></script>
		<script src="../../../js/common.js"></script>
		<script src="../../../js/utils/base64.js"></script>
		<script src="../../../js/utils/md5.js"></script>
		<script src="../../../js/utils/sortMap.js"></script>
		<script src="../../../js/utils/http.js"></script>
		<script src="../../../js/app.js"></script>
		<script src="../../../js/sqliteTools.js"></script>
		<!-- 基础js end -->

		<script type="text/javascript" charset="utf-8">
			var ServiceNoticeId
			var serviceNo
			var planId
			var planState
			
			// 仪器详情用带过去
			var trainQuota
			var installationDate
			var cfda
			
			// 服务通知详细信息用
			var serviceType
			var isWarranty
			var troubleDescription
			var remark
			var serviceRole
			var subServiceType
			var latitude
			var longitude
			var oldPlanId
			var noticeId
			var orderReceivedDate
			var problem
			var divBu
			var customerName
			
			// 取消方法 关闭弹窗
			function closepop(){
				mui("#popover").popover('hide', document.getElementById("div"));
			}
			
			(function($, doc) {
				$.init();
				
				$.plusReady(function() {
					// 从上一个页面取值
					var self = plus.webview.currentWebview();
					 // 获取上一个页面传过来的参数并显示到页面
					 ServiceNoticeId = self.ServiceNoticeId;
					 serviceNo = self.serviceNo;
					 planId = self.planId;
					 					 
					 // 获取定位的经纬度
					 app.showWaiting('加载中...');
					 plus.geolocation.getCurrentPosition(function(p){
					 	longitude = p.coords.longitude;
					 	latitude = p.coords.latitude;
						// 获取详情的方法
						getDetails();
					 }, function(e){
						app.closeWaiting();
						app.toast("定位失败");
					 }, {geocode: false});
					 
					 
					 
					 // 客户详细信息
					 h("#khxxxx").tap(function(){
						 app.jumpTo("/view/service/accept/customerDetails.html", "view/service/accept/customerDetails.html",
						 {
						 	ServiceNoticeId:ServiceNoticeId,
						 	serviceNo: serviceNo,
						 	planId: planId
						 });
					 })
					 
					 // 合同详细信息
					 h("#htxx").tap(function(){
						// app.toast("暂未开放");
						app.jumpTo("/view/service/accept/contractDetails.html", "view/service/accept/contractDetails.html",{
							serviceNo: serviceNo,
						});
					 })
					 
					 // 仪器详细信息
					 h("#yqxx").tap(function(){
						 app.jumpTo("/view/service/accept/instrumentDetails.html", "view/service/accept/instrumentDetails.html",
						 {
						 	ServiceNoticeId:ServiceNoticeId,
						 	serviceNo: serviceNo,
						 	planId: planId,
							trainQuota: trainQuota,
							installationDate: installationDate,
							cfda: cfda
						 });
					 })
					 
					 // 服务通知详细信息
					 h("#fwtzxx").tap(function(){
						 app.jumpTo("/view/service/accept/noticeDetails.html", "view/service/accept/noticeDetails.html",
						 {
						 	ServiceNoticeId:ServiceNoticeId,
						 	serviceNo: serviceNo,
						 	planId: planId,
							serviceType: serviceType,
							isWarranty: isWarranty,
							troubleDescription: troubleDescription,
							remark: remark
						 });
					 })
					 
					 // 推荐使用备件详细信息
					 h("#tjsybjxx").tap(function(){
						 app.jumpTo("/view/service/accept/partDetails.html", "view/service/accept/partDetails.html",
						 {
						 	ServiceNoticeId:ServiceNoticeId,
						 	serviceNo: serviceNo,
						 	planId: planId
						 });
					 })
					 
					 // 更改预计上门时间
					 h("#ggyjsmsj").tap(function(){
						 app.jumpTo("/view/service/accept/expectedDoorUpdate.html", "view/service/accept/expectedDoorUpdate.html",
						 {
							ServiceNoticeId:ServiceNoticeId,
							serviceNo: serviceNo,
							planId: planId,
							serviceRole: serviceRole
						 });
					 })
					 		
					 // 信息更改申请
					 h("#xxggsq").tap(function(){
						 app.jumpTo("/view/service/accept/infoUpdate.html", "view/service/accept/infoUpdate.html",
						 {
							ServiceNoticeId: ServiceNoticeId,
							serviceNo: serviceNo,
							noticeId: ServiceNoticeId,
							planId: planId
						 });
					 })
					 
					 // 查看日志
					 h("#ckrz").tap(function(){
					 	app.jumpTo("/view/service/serviceLog/noticeLogList.html", "view/service/serviceLog/noticeLogList.html",
					 	{
					 		serviceNo: serviceNo,
							planId: planId
					 	});
					 })
					 // 查看报告
					 h("#ckbg").tap(function(){
					 	app.jumpTo("/view/service/report/work/workReportList.html", "view/service/report/work/workReportList.html",
					 	{
					 		serviceNo: serviceNo,
							planState : planState
					 	});
					 })
					 // 合同详细信息
					 h("#htxx").tap(function(){
						 app.jumpTo("/view/service/accept/contractDetails.html", "view/service/accept/contractDetails.html",{
							serviceNo: serviceNo,
						 });
					 })
					 
					 // Error Report&Hot Account Report
					 h("#report").tap(function(){
						 app.jumpTo("/view/service/report/errorhot/erroOrHot.html", "view/service/report/errorhot/erroOrHot.html",
						 {
							serviceNo: serviceNo,
							serviceType: serviceType,
							planId: planId,
							noticeId:ServiceNoticeId,
							divBu: divBu,
							problem: problem
						 });
					 })
				});
				
				// 获取详情的方法
				function getDetails(){
					ajaxGet(BASE_URL + "cc/notice/getMyServiceNoticeDetail", {
						"ServiceNoticeId": ServiceNoticeId,
						"EnginnerId": app.getUser().enginnerId
					}, function(data) {
						app.closeWaiting()
						if (data.code == 200) {
							if(data.data && data.data.length > 0){
								h("#serviceNo").html(data.data[0].serviceNo);
								h("#createTime").html(data.data[0].createTime);
								h("#serviceType").html(data.data[0].subServiceType);
								planState = data.data[0].planState;
								noticeId = data.data[0].id;
								isWarranty = data.data[0].isWarranty;
								problem = data.data[0].troubleDescription;
								divBu = data.data[0].divBu;
								h("#customerName").html(data.data[0].customerName);
								customerName = data.data[0].customerName;
								if(data.data[0].enginnerType==undefined || data.data[0].enginnerType==""){
									h("#enginnerType").html("无");
								}else{
									h("#enginnerType").html(data.data[0].enginnerType);
								}
								if(data.data[0].hopeBusinessHours==undefined || data.data[0].hopeBusinessHours==""){
									h("#hopeBusinessHours").html("无");
								}else{
									h("#hopeBusinessHours").html(data.data[0].hopeBusinessHours);
								}
								
								// 仪器详细信息页面用
								if(data.data[0].trainQuota==undefined || data.data[0].trainQuota==""){
									trainQuota = "";
								}else{
									trainQuota = data.data[0].trainQuota;
								}
								if(data.data[0].installationDate==undefined || data.data[0].installationDate==""){
									installationDate = "";
								}else{
									installationDate = data.data[0].installationDate;
								}
								if(data.data[0].cfda==undefined || data.data[0].cfda==""){
									cfda = "";
								}else{
									cfda = data.data[0].cfda;
								}
								
								// 服务通知详细信息页面用
								if(data.data[0].serviceType==undefined || data.data[0].serviceType==""){
									serviceType = "";
								}else{
									serviceType = data.data[0].serviceType;
								}
								if(data.data[0].subServiceType==undefined || data.data[0].subServiceType==""){
									subServiceType = "";
								}else{
									subServiceType = data.data[0].subServiceType;
								}
								if(data.data[0].isWarranty==undefined || data.data[0].isWarranty==""){
									isWarranty = "";
								}else{
									isWarranty = data.data[0].isWarranty;
								}
								if(data.data[0].troubleDescription==undefined || data.data[0].troubleDescription==""){
									troubleDescription = "";
								}else{
									troubleDescription = data.data[0].troubleDescription;
								}
								if(data.data[0].remark==undefined || data.data[0].remark==""){
									remark = "";
								}else{
									remark = data.data[0].remark;
								}
							
								serviceRole = data.data[0].serviceRole
								if (serviceRole) {
									 document.querySelector(`input[name='serviceRole'][value='${serviceRole}']`).checked=true;
								}
								selectOldPlanDetailId();
							} else {
								app.toast('查询失败')
							}
						} else {
							app.toast('查询失败')
						}
					}, function(errorMsg) {
						app.closeWaiting();
						app.toast('查询失败')
					});
				}
				
				// 签到
				h("#qd").tap(function(){
					app.showWaiting('正在提交...')
					ajaxGet(BASE_URL + "cc/user/checkUserWorkDay", {"enginnerId": app.getUser().enginnerId}, function(data) {
						if (data.code == 200) {//查询成功
							var xiujia = data.data[0].workday==undefined?"":data.data[0].workday;
							if(xiujia=="3"){
								app.closeWaiting()
								mui("#popover").popover('toggle', document.getElementById("div"));
							}else{
								// 调用签到方法
								qiandao();
							}
						} 
					}, function(errorMsg) {
					});
				})
				
				// 弹窗确定按钮操作
				h("#quedingupdate").tap(function(){
					ajaxGet(BASE_URL + "cc/user/updateUserWorkday", {
						"enginnerId": app.getUser().enginnerId,
						"workday": "1"
					}, function(data) {
						if (data.code == 200) {//查询成功
							qiandao();
						}else{
							app.toast(data.msg);
						}
						mui("#popover").popover('toggle', document.getElementById("div"));
					}, function(errorMsg) {
					});
				})
				
				// 添加报告
				h("#tjbg").tap(function(){
					// app.jumpTo("/view/service/report/maintenanceReport.html", "view/service/report/maintenanceReport.html",
					// {
					// 	serviceNo: serviceNo,
					// 	serviceType: serviceType,
					// 	subServiceType: subServiceType,
					// 	fseName: app.getUser().name
					// });
					//根据planID 去查询接受任务时间
					 ajaxGet(BASE_URL + "cc/PlanDetails/getDetailsById", {
					 	"id": oldPlanId
					 }, function(data) {
					 	if(data.code==200){
							orderReceivedDate = data.data.acceptTime
						}else{
							app.toast('获取接受任务时间失败');
						}
					 }, function(errorMsg) {
						 app.toast('获取接受任务时间失败');
					 });
					 
					app.jumpTo("/view/service/report/maintenanceReport.html", "view/service/report/maintenanceReport.html",
					{
						serviceNo: serviceNo,
						planId: oldPlanId,
						planState : planState,
						noticeId: noticeId,
						orderReceivedDate:orderReceivedDate,
						isWarranty: isWarranty,
						serviceType:serviceType,
						subServiceType: subServiceType,
						problem: problem
					});
				})
				
				h("#tjrz").tap(function(){
					app.jumpTo("/view/service/serviceLog/logAdd.html", "view/service/serviceLog/logAdd.html",
					{
						serviceNo: serviceNo,
						planId:oldPlanId,
						serviceNoticeId: ServiceNoticeId,
						listName: 'signInAddReport'
					});
				})
				
			}(mui, document));
			
			function qiandao(){ 
				// 判断上一个计划是否写过日志，没写过日志不允许签到
				ajaxPost(BASE_URL + "cc/ServiceLog/selectServiceLogList", {
					"serviceNoticeId": ServiceNoticeId,
					"stPlanId": oldPlanId
				}, function(data) {
					if(data.code === 200){
						if(data.data && data.data.length == 0){
							app.toast("暂无法签到，请先添加日志")
							app.closeWaiting()
							return
						}
						
						//判断有没有服务通知列表
						ajaxGet(BASE_URL + "cc/notice/AssociatedCustomers", {
							"ServiceNoticeId": ServiceNoticeId,
							"EnginnerId": app.getUser().enginnerId,
							"state": 1,
							// "planId": planId
						}, function(data) {
							if(data.code==200){
								if(data.data && data.data.length > 1){  //跳服务通知页
									app.closeWaiting()
									app.jumpTo("/view/service/notice/relationNotices.html", "view/service/notice/relationNotices.html",
									{
										serviceNo: serviceNo,
										planId: planId,
										ServiceNoticeId: ServiceNoticeId,
										state: 1
									});
								}else{
									//判断地址是否有变更  
									ajaxGet(BASE_URL + "cc/instrument/checkAddressDistance", {
										"ServiceNoticeId": ServiceNoticeId,
										"longitude": longitude,
										"latitude": latitude
									}, function(data) {
										app.closeWaiting()
										if(data.code==200){
											if(data.data){ //跳转地址变更页
												// app.jumpTo("/view/service/addressChange/addressChange.html", "view/service/addressChange/addressChange.html",
												// {
												// 	serviceNos: serviceNo,
												// 	ServiceNoticeIds: ServiceNoticeId,
												// 	noticeId: ServiceNoticeId
												// });	
												var addressChange = plus.webview.create('/view/service/addressChange/addressChange.html','view/service/addressChange/addressChange.html',null,
												{
													serviceNo: serviceNo,
													serviceNos: serviceNo,
													ServiceNoticeIds: ServiceNoticeId,
													planIds: planId,
													noticeId: ServiceNoticeId,
													customerName: customerName
												})
												addressChange.show()
											}else{
												qiandao6()
											}
										}else{
											 app.toast(data.msg)
										}
									}, function(errorMsg) {
										app.closeWaiting()
										app.toast(errorMsg)
									});
								}
							}else{
								app.closeWaiting()
								app.toast("签到失败")
							}
						}, function(errorMsg) {
							app.closeWaiting()
						});
						
					}else{
						app.closeWaiting()
						app.toast('查询失败')
					}
				}, function(errorMsg) {
					app.closeWaiting()
					app.toast('查询失败')
				});
				
			}
			
			// 有下次预计上门查询最后一次签离的ID
			function selectOldPlanDetailId(){
				ajaxGet(BASE_URL + "cc/PlanDetails/selectAllOldPlanDetailId", {
					"serviceNoticeId": ServiceNoticeId,
					"enginnerId": app.getUser().enginnerId
				}, function(data) {
					if(data.code == 200){
						oldPlanId = data.data && data.data.id ? data.data.id : '';
						judgeLog();
					}else{
						app.toast('查询失败')
					}
				}, function(errorMsg) {
					app.toast('查询失败')
				});
			}
			
			function judgeLog(){
				ajaxPost(BASE_URL + "cc/ServiceLog/selectServiceLogList", {
					"serviceNoticeId": ServiceNoticeId,
					"stPlanId": oldPlanId
				}, function(data) {
					if(data.code === 200){
						if(data.data && data.data.length > 0){  //显示添加报告
							// 实习生不能添加报告
							if(serviceRole=='1'){
								h('#tjbg').show();
								h('#tjrz').hide();
							} else {
								// 没按钮被提bug，那就让他有按钮呗
								h('#tjbg').hide();
								h('#tjbg').attr('disabled','disabled');
								h('#tjrz').hide();
							}
						}else{ 	//显示添加日志
							h('#tjrz').show();
							h('#tjbg').hide();
						}
					}else{
						// 没按钮被提bug，那就让他有按钮呗
						h('#tjrz').show();
						h('#tjrz').attr('disabled','disabled');
						h('#tjbg').hide();
						app.toast('获取日志列表失败')
					}
				}, function(errorMsg) {
					// 没按钮被提bug，那就让他有按钮呗
					h('#tjrz').show();
					h('#tjrz').attr('disabled','disabled');
					h('#tjbg').hide();
					app.toast('获取日志列表失败')
				});
			}
			
			function qiandao6(){
				ajaxGet(BASE_URL + "cc/PlanDetails/fseSign", {
					"relServiceNo": serviceNo,
					"ServiceNoticeIds": ServiceNoticeId,
					"serviceNos": serviceNo,
					"planIds": planId,
					"EnginnerId": app.getUser().enginnerId,
					"longitude": longitude,
					"latitude": latitude
				}, function(data) {
					app.closeWaiting()
					if(data.code==200){ 
						// 判断是否存在离线数据，
						var enginnerId = app.getUser().enginnerId;
						var fseName = app.getUser().name;
						var sqliteTool = new sqliteTools(); 						
						var resData = data.data[0];
						sqliteTool.openDatabase();
						sqliteTool.selectServiceByPlanId(enginnerId, resData.planId, resData, function(flag, offlineData, callBackData) {
							if(flag){
								if (offlineData && offlineData.length > 0) {
									sqliteTool.insertServiceSignInData(offlineData[0].id, "1", callBackData.signInTime, longitude, latitude, "0", function(flag) {
										console.log("更新本地签到数据"+flag)
										sqliteTool.closeDB();
										app.toast("签到成功")
										tiaozhuan()
									})
								} else {
									sqliteTool.insertServiceBaseAndSignInData(callBackData.serviceNo, callBackData.serviceNoticeId, callBackData.planId, customerName, callBackData.instrumentId, callBackData.instSn, callBackData.acceptTime, enginnerId, fseName, callBackData.serviceType, callBackData.subServiceType, "1", callBackData.signInTime, longitude, latitude, function (flag) {
										console.log("插入本地签到数据"+flag)	
										sqliteTool.closeDB();
										app.toast("签到成功")
										tiaozhuan()
									})						
								}
							}						
						});					
					}else{
						app.toast(data.msg)
					}
				}, function(errorMsg) {
					app.closeWaiting()
					app.toast(errorMsg)
				});
			}
			
			function tiaozhuan(){
				var signIn = plus.webview.getWebviewById('view/service/notice/signIn.html');
				if (signIn) {
					signIn.close()
				}
				
				// 关闭进行中
				var conductList = plus.webview.getWebviewById('view/service/assign/conductList.html');
				if(conductList){
					conductList.close()
				}
				
				// 关闭工作台已签离列表
				var checkedOutList = plus.webview.getWebviewById('view/service/notice/checkedOutList.html');
				if(checkedOutList){
					checkedOutList.close()
				}
				
				// 关闭工作台查询未关闭服务通知列表
				var notClosedList = plus.webview.getWebviewById('view/service/notice/notClosedList.html');
				if(notClosedList){
					notClosedList.close()
				}
				
				// 刷新TL首页
				var indexTl=plus.webview.getWebviewById('view/main/indexTL.html');
				if(indexTl){
				    indexTl.evalJS('getMyTeamCount()')
				}
				
				// 1、刷新FSE首页
				var indexFSE = plus.webview.getWebviewById('view/main/indexFSE.html');
				if(indexFSE){
					indexFSE.evalJS('getList("1")')
				}
				
				// 关闭工作台已接受列表
				var acceptedList = plus.webview.getWebviewById('view/service/notice/acceptedList.html');
				if(acceptedList){
					acceptedList.close()
				}
				
				var self = plus.webview.currentWebview();
				 // 查找需要跳转的页面，并指定刷新的方法
				var checkedInList = plus.webview.getWebviewById('view/service/notice/checkedInList.html');
				if(checkedInList){
					checkedInList.evalJS('initData()')
					mui.back()
				}else{
					var checkedInList1 = plus.webview.create('/view/service/notice/checkedInList.html','view/service/notice/checkedInList.html')
					checkedInList1.show()
					self.close();
				}
				
			}
		</script>
		<!-- js代码 end -->

	</body>

</html>
